/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EntityEdit.java
 *
 * Created on 11-jul-2011, 17:51:39
 */
package pfc.gui;

import javax.swing.table.DefaultTableModel;
import org.apache.ddlutils.model.Database;
import pfc.arbol.CGTreeNode;
import pfc.comun.IDEpfc;
import pfc.interfaces.Salvable;
import pfc.modelocg.CGEntityForms;
import pfc.modelocg.CGForm;
import pfc.modelocg.CGModel;
import pfc.modelocg.CGModelIO;
import pfc.modeloer.ERModel;

/**
 *
 * @author xandru
 */
public class CGModelEdit extends Editor implements Salvable {

    CGTreeNode _nodo;

    public CGModelEdit(CGTreeNode aThis) {
        this._nodo = aThis;

        initComponents();
        loadData();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Entidad", "Form Edicion", "Form Lineas", "Form Busqueda", "Form Enlazados"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, true, true, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("Cerrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Aplicar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 625, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(180, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addGap(140, 140, 140)
                .addComponent(jButton1)
                .addGap(173, 173, 173))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(214, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
         this.setVisible(false);
        this.getParent().remove(this);
        IDEpfc.app.getIDE().removeChanged(this);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
       System.out.println("Guardando configuracion CGModel");

       save();
    }//GEN-LAST:event_jButton2ActionPerformed

    
    public void save() {
        CGModel m = this._nodo.getCGModel();

        String fichero = this._nodo.getCodeGenerationFile();

        DefaultTableModel tm = (DefaultTableModel) jTable1.getModel();
        for (int row=0; row<tm.getRowCount();row++)
        {
            String entityName = (String) tm.getValueAt(row, 0);
            Boolean genEdit= (Boolean) tm.getValueAt(row, 1);
            Boolean genLines= (Boolean) tm.getValueAt(row, 2);
            Boolean genSearch= (Boolean) tm.getValueAt(row, 3);
            Boolean genLink= (Boolean) tm.getValueAt(row, 4);

            CGEntityForms ef = m.findEntityForms(entityName);
            if (ef!=null){
                ef.setGenerateEditForm(genEdit);
                ef.setGenerateLinesForm(genLines);
                ef.setGenerateLinkedForms(genLink);
                ef.setGenerateSearchForm(genSearch);
            }
            else
               throw new UnsupportedOperationException("Error buscando la entidad " + entityName + " en el modelo de generacion de codigo");
        }

        CGModelIO.model2XML( m , fichero);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

    private void loadData() {

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {},
            new String [] {"Entidad", "Form Edicion", "Form Lineas", "Form Busqueda", "Form Enlazados"}
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, true, true, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });

        DefaultTableModel m = (DefaultTableModel) jTable1.getModel();

        for (CGEntityForms ef: this._nodo.getCGModel().getEntityForms())
        {
            Object[] rowData= {ef.getEntityName(),ef.isGenerateEditForm(),ef.isGenerateLinesForm(),ef.isGenerateSearchForm(),ef.isGenerateLinkedForms()};
            m.addRow(rowData);
        }


    }


}
